package com.lry.netty01.controller;

import com.lry.netty01.server.Server;
import com.lry.netty01.server.entity.AjaxResult;
import com.lry.netty01.server.entity.FixedFrame;
import com.lry.netty01.server.entity.Packet;
import com.lry.netty01.server.queue.Task;
import com.lry.netty01.server.utils.ChannelUtil;
import com.lry.netty01.server.utils.CommandUtil;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("degree")
public class DegreeController {

    // http://localhost:8080/degree/setupDegree/1/12

    @RequestMapping("setupDegree/{devId}/{degree}")
    @ResponseBody
    public AjaxResult setupDegree(@PathVariable short devId, @PathVariable byte degree){
        Channel channel = ChannelUtil.online_channels2.get(devId);
        if(!ChannelUtil.checkChannel(channel)){
            return AjaxResult.error(devId+"不在线");
        }

        try {
            FixedFrame resp = CommandUtil.setupDegreeResult(devId,degree);

            return AjaxResult.success(resp);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return AjaxResult.error("发送命令失败或请求超时");
    }

}
